﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using NHibernate.Mapping;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.WorkFlow.Entities;

namespace VitCode.WorkFlow.DataAccess
{
    public class WorkFlowAccess : BaseDataAccess
    {
        /// <summary>
        /// 获取下一级审批的环节集合
        /// </summary>
        /// <returns></returns>
        public WorkItem[] GetNextWorkItems()
        {

            return null;
        }

        /// <summary>
        /// 获取上一级审批的环节集合
        /// </summary>
        /// <returns></returns>
        public WorkItem[] GetPevWorkItems()
        {

            return null;
        }


        //public ArrayList GetWorkItemByActivityId(Guid activityId)
        //{
        //    var reVal = new ArrayList();
        //    SqlHelper sqlHlp = new SqlHelper();
        //    string sql = "select p.Id from workflow_workitem p "
        //                 + "left join WorkFlow_WorkItemRelation cr  "
        //                 + "on cr.ParentItemId=p.Id "
        //                 + "left join dbo.WorkFlow_WorkItem c  "
        //                 + "on c.Id=cr.ChildItemId "
        //                 + "where p.ActivityId='"+activityId+"' "
        //                 + "order by p.LevelNum,c.Name";
        //    DataSet ds = sqlHlp.ExecuteDataSet(Session, sql);
        //    var sc = new StringCollection();
        //    if(ds!=null && ds.Tables.Count>0)
        //    {
        //        DataTable dt = ds.Tables[0];
        //        foreach(DataRow dr in dt.Rows)
        //        {
        //            string val = Convert.ToString(dr[0]);
        //            if(!sc.Contains(val))
        //                sc.Add(val);
        //        }
        //    }
        //    if(sc.Count>0)
        //    {
        //        var nd = GetObjectsByKeyValues(typeof(WorkItem).Name, "Id", sc);
        //        if(nd!=null && nd.Count>0)
        //        {
        //            for(var i=0;i<sc.Count;i++)
        //            {
        //                if(nd.Contains(sc[i]))
        //                {
        //                    reVal.Add(nd[sc[i]]);
        //                }
        //            }
        //        }
        //    }
        //    return reVal;
        //}

        public IList GetWorkItemsByActivityId(string activityId)
        {
            return Session.CreateQuery("From " + typeof(WorkItem).Name + " o Where o.Activity.Id='" + activityId + "'").List();
        }

        public IList GetWorkItemRelationsByActivityId(string activityId)
        {
            return Session.CreateQuery("From "+typeof (WorkItemRelation).Name+" o Where o.ParentItem.Activity.Id='" + activityId
                                                                       + "' or o.ChildItem.Activity.Id='" + activityId +
                                                                       "'").List();
        }
    }
}
